﻿/*
* Activity Tracking Log v0.1.1 - http://activitytrackinglog.codeplex.com
* Activity Tracking Log for ASP.Net & ASP.Net MVC
* Copyright (c) 2011 Vadim Burac
* Licensed under the Microsoft Public License (Ms-PL)
* http://activitytrackinglog.codeplex.com/license
*/

using System;
using System.Web;
using ActivityTrackingLog.Utils;

namespace ActivityTrackingLog.Web.Handlers
{
    internal class ClientSideLogClient : BaseHandler, IHttpHandler
    {
        #region IHttpHandler Members

        public bool IsReusable
        {
            // Return false in case your Managed Handler cannot be reused for another request.
            // Usually this would be false in case you have some state information preserved per request.
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            string securityKey = base.GetSecurityKey();
            string responseBody = string.Empty;
            if (context.Request.RequestType == "GET" && !string.IsNullOrWhiteSpace(securityKey))
            {
                responseBody = this.ServeJs(securityKey);
            }
            context.Response.ContentType = "text/javascript";
            context.Response.ContentEncoding = System.Text.UTF8Encoding.Default;
            context.Response.Write(responseBody);
            context.Response.End();
        }

        #endregion

        #region helpers

        private string ServeJs(string securityKey)
        {
            var template = ResourceHelper.GetClientLogProxyJs();
            return template
                .Replace("{security_key}", securityKey)
                .Replace("{log_handler_url}", Configuration.Config.ClientSideAccess.HandlerUrl);
        }

        #endregion
    }
}
